import sys
sys.path.append("../")
from model.CommonDb import *
from services.BaseService import *
from frameworks.utils.FileUtil import *
import pandas as pd
import datetime

class ZhangTingService(BaseService):
    def __init__(self):
        super(ZhangTingService, self).__init__()
        self.db = CommonDb("zhangting","stock")
        self.db1 = CommonDb("codes", "stock")

    def getZhangting(self,orderKey):
        sql = "select code from day_kline_"+str(orderKey)+" where trade_date >= (select max(trade_date) " \
                                                          "from day_kline_9 where code='SH600009') and close/pre_close > 1.09 and " \
                                                          "concat(code,trade_date) not in (select concat(code,trade_date) from zhangting)"
        print(sql)
        return self.db.execute(sql)

    def init(self):
        sql = "truncate table zhangting"
        self.db.executeUpdate(sql)

    def insertZhangting(self,info):
        self.db.add(info)

    def getAllZhangting(self):
        return self.db.selectAll("1","*",True)

    def getOneZhangting(self):
        sql = "select b.* from zhangting a left join codes b on a.code=b.code where a.lianban_num=1 and b.flow_money < 10000000000";
        return self.db1.executeMap(sql)

    def getZhangtingInfo(self,num):
        sql = "select b.* from zhangting a left join codes b on a.code=b.code where a.lianban_num="+str(num)+";"
        return self.db1.executeMap(sql)

    def getMoreZhangTing(self,num):
        if int(num) > 3:
            return self.db.selectAll("lianban_num > 3 and logday in (select max(logday) from zhangting) order by lianban_num desc","*",True)
        else:
            return self.db.selectAll("lianban_num = "+str(num)+" and logday in (select max(logday) from zhangting)","*",True)